/****************************************************************************
* 			Title: STYL Paper MAIN TABLES
*			Output:
*			Date:
*****************************************************************************/

////1. DIRECTORY INFORMATION ////
*clear all
*set maxvar 30000
*use "data\clean\STYL_lr_panel_analysis.dta", clear


////2. ADDITIONAL SET UP ////
set varabbrev off
set matsize 1000

//// 3. SURVEYSET, AND FUNCTIONS ////

    qui include dofiles/functions/multivariate_reg.do
    qui include dofiles/functions/ver_itt_table.do
    qui include dofiles/functions/label_itt_table.do
    qui include dofiles/functions/reg_tests.do
    qui include dofiles/functions/matrices.do


 	//c. SET SURVEY

sort partid round
svyset, strata(strata) psu(partid)


		/* Generate fixed effects dummies */
			qui tab tp_strata_alt, g(tp_dummy)
			qui tab cg_strata, g(cg_dummy)
			qui tab neighborhoodphase, g(nbhd_dummy)


        cap drop __000001
        cap drop __000000




/// TABLE 1 .-

    gl deaths_plus_cens_index_asb  $cens_index_asb   ///
                                   any_death         ///
                                   violent_death     ///
                                   illness_death



gen any_death_tyav =  (attrit_both_decease_9y == 1)  if round == 7
cap gen violent_death_tyav     = any_death_tyav == 1 & (updated_death_type == "Violent Death")  if round == 7
cap gen illness_death_tyav     = any_death_tyav == 1 & (updated_death_type == "Illness")  if round == 7


label variable any_death_tyav                 "Any death"
label variable violent_death_tyav             "\quad{Violent death}"
label variable illness_death_tyav             "\quad{Health complications}"


vert_parm_reg if inlist(round,7) , outcomevars($deaths_plus_cens_index_asb)  controls($base) fixedeffects(tp_strata_alt cg_strata)  filename(Table1_main_plus_attr)


/// TABLE 2.-

gen secondary_header_tyav =.
gen secondary_header_stav =.

label variable cens_fam_asb_tyav             "Antisocial Behaviors"
label variable secondary_header_tyav             "Secondary outcomes:"

label variable fam_econ_tyav             "\quad{Economic performance}"
label variable timepref_tyav             "\quad{Forward-looking time preferences}"
label variable selfcontrolnolo_tyav      "\quad{Self-control skills}"
label variable fam_identity_tyav         "\quad{Identity and values}"
label variable fam_mental_tyav           "\quad{Positive self-regard/mental health}"
label variable fam_subabuse_tyav         "\quad{Substance abuse}"
label variable fam_network_tyav          "\quad{Quality of social networks}"


round_test_reg_NSM  if unfound_wave==0, outcomevars(cens_fam_asb secondary_header fam_econ timepref selfcontrolnolo fam_identity fam_mental fam_subabuse fam_network) ///
        treatvars(tpassonly cashassonly tpcashass) controls($base) fixedeffects(tp_strata_alt cg_strata) ///
        rounds(5 7) wavenames(ltav tyav) roundvar(round) wavevar(nyel) filename(Table2_LTvs10Y) repooled partid(partid) col_0_title("Outcome index (z-score)")


label variable fam_econ_tyav             "Economic performance"
label variable timepref_tyav             "Forward-looking time preferences"
label variable selfcontrolnolo_tyav      "Self-control skills"
label variable fam_identity_tyav         "Identity and values"
label variable fam_mental_tyav           "Positive self-regard/mental health"
label variable fam_subabuse_tyav         "Substance abuse"
label variable fam_network_tyav          "Quality of social networks"


/// TABLE 3 [copied from previous paper. Not done in this script]


/////// ONLINE APPENDIX ************************************************************

/// A.- Additional information on experimental procedures

/// TABLE A1.-

qui {
    gen block=.
    replace block=1 if redlight_p1==1
    replace block=2 if redlight_p2==1
    replace block=3 if centralmonrovia_p2==1
    replace block=4 if claratown_p3==1
    replace block=5 if logantown_p3==1
    replace block=6 if newkrutown_p3==1
    label define block 1 "Red light" 2 "Red light, second phase" 3 "Central Monrovia" 4 "Clara Town" 5 "Logan Town" 6 "New Kru Town"
    label values block block

    replace phase=3 if phase==0

   gen status_treatment=.
   replace status_treatment=1 if tpassonly==1
   replace status_treatment=2 if cashassonly==1
   replace status_treatment=3 if tpcashass==1
   replace status_treatment=4 if control==1
   label define status_treatment 1 "Therapy" 2 "Cash" 3 "Therapy + Cash" 4 "Neither"
   label values status_treatment status_treatment

}

* Original Table (2017)
                    tab block status_treatment if round==1,row

                    mat drop _all
                    forvalues x=1(1)6 {
                        tabmat status_treatment if block==`x' & round==1, matrix(mat`x')
                    }

                    tabmat2 block if round==1, matrix(mat0)

                    mat results_a = mat1 , mat2 , mat3 , mat4 , mat5 , mat6
                    mat results_b=results_a'
                    matrix colnames mat0 = No.
                    mat results1= mat0 , results_b

                    mat lis results1

* updated version
                    tab block status_treatment if round==7 & unfound_wave ==0,row

                    forvalues x=1(1)6 {
                        tabmat status_treatment if block==`x' & round==7 & unfound_wave==0, matrix(mat`x')
                    }

                    tabmat2 block if round==7 & unfound_wave==0, matrix(mat0)

                    mat results_a = mat1 , mat2 , mat3 , mat4 , mat5 , mat6
                    mat results_b= results_a'
                    matrix colnames mat0 = No.
                    mat results2= mat0 , results_b

                    mat lis results2


mat results=results1 , results2
mat lis results

frmttable, statmat(results) sdec(0,1,1,1,1,0,1,1,1,1) varlabels

frmttable using outfiles/tables/TableA1_random_block, varlabels ///
ctitle("","\uline{\hfill Round 1 \hfill}","","","","","\uline{\hfill Round 7 \hfill}","","","","" \ ///
        "","\thead{\$ # \$}","\thead{Therapy (\%)}","\thead{Cash (\%)}","\thead{Therapy \\ \+ cash (\%)}", "\thead{Control (\%)}", "\thead{\$ # \$}","\thead{Therapy (\%)}","\thead{Cash (\%)}","\thead{Therapy \\ \+ cash (\%)}", "\thead{Control (\%)}") ///
        tex fragment replace nocenter squarebrack ///
        multicol(1,2,5;1,7,5)


/// TABLE A2.-  ***********************************************

count if round == 7 & unfound_wave == 0
local my_N = r(N)

local B = `:word count $base3'
mat Balance10Y = J(`B',8,.)

local my_fontgrid

local row  = 1
foreach x in $base3 {



multivar_reg_mat if round == 7 & unfound_wave == 0, outcomevar(`x') ///
                  controls(tpassonly cashassonly tpcashass) ///
                  fixedeffects(tp_strata_alt cg_strata) ///
                  matname("bal_`x'")


qui test tpassonly = cashassonly = tpcashass = 0
        local F_`x' = r(p)


*Sample Mean or Control Mean ???
qui summ `x' if round == 7 & unfound_wave == 0
*& treatment == 0
        local `x'_cons = r(mean)


mat Balance10Y[`row', 1] = ``x'_cons'
mat Balance10Y[`row', 2] = bal_`x'[1,1]
mat Balance10Y[`row', 3] = bal_`x'[1,3]
mat Balance10Y[`row', 4] = bal_`x'[2,1]
mat Balance10Y[`row', 5] = bal_`x'[2,3]
mat Balance10Y[`row', 6] = bal_`x'[3,1]
mat Balance10Y[`row', 7] = bal_`x'[3,3]
mat Balance10Y[`row', 8] = `F_`x''


    local p_value_therapy = cond(bal_`x'[1,3] <= 0.05, "bf", "normalsize")
    local p_value_cash    = cond(bal_`x'[2,3] <= 0.05, "bf", "normalsize")
    local p_value_both    = cond(bal_`x'[3,3] <= 0.05, "bf", "normalsize")
    local p_value_F       = cond(`F_`x'' <= 0.05     , "bf", "normalsize")


   local this_font_row   "  normalsize, normalsize, `p_value_therapy', normalsize, `p_value_cash', normalsize, `p_value_both', `p_value_F' "


if `row' == 1 local my_fontgrid "`this_font_row'"
else local my_fontgrid "`my_fontgrid' \ `this_font_row'"


local row = `row' + 1
}


mat rownames Balance10Y = $base3
matlist Balance10Y

qui frmttable, statmat(Balance10Y) sdec(3) varlabels

frmttable using outfiles/tables/TableA2_balance, ///
tex fragment replace nocenter squarebrack ///
ctitle("", "", "\uline{\hfill Test of randomization balance(N=`my_N') \hfill}", "", "", "", "", "", "" \ ///
"", "Sample", "\uline{\hfill Assigned Therapy \hfill}", "", "\uline{\hfill Assigned Cash \hfill}", "", "\uline{\hfill Assigned Both \hfill}", "", "F-Test" \ ///
"Baseline covariate", "Mean", "Coef.", "p value", "Coef.", "p value", "Coef.", "p value", "p value" \ ///
"", "(1)","(2)","(3)","(4)","(5)","(6)", "(7)", "(8)") ///
    mu(1,3,7;  2,3,2;  2,5,2; 2,7,2) ///
    statfont("`my_fontgrid'")



//////// TABLE A3.- **********************************


multivar_reg_mat if inlist(round,7), outcomevar(Unfound_9YEL) ///
                  controls(tpassonly cashassonly tpcashass $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(bal_att_unfound)

multivar_reg_mat if inlist(round,7), outcomevar(attrit_both_decease_9y) ///
                  controls(tpassonly cashassonly tpcashass $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(bal_att_mortality)

multivar_reg_mat if inlist(round,7), outcomevar(attrit_both_imprisonment_9y) ///
                  controls(tpassonly cashassonly tpcashass $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(bal_att_prison)


*Unfound_9YEL_allnomort basically takes Unfound_9YEL but excuses (codes as 0 also all the attrit_both_decease_9y or attrit_both_imprisonment_9y)


multivar_reg_mat if inlist(round,7), outcomevar(Unfound_9YEL_allnomort) ///
                  controls(tpassonly cashassonly tpcashass $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(bal_att_unfoundnom)



mv_reg_table, resultsmat( ///
                         bal_att_unfound           ///
                         bal_att_mortality     ///
                         bal_att_prison        ///
                         bal_att_unfoundnom        ///
                         ) ///
                rowvars(tpassonly cashassonly tpcashass $base_small ) ///
              stars stats(dvmean N) novalues(yes) ///
              specnames(`" "Did not survey"  "Deceased" "Imprisoned" "Did not survey \\ (Excl. deceased \\ \& imprisoned)"    "') ///
              filename(TableA3_attrition_death)


//// TABLE A4.-
inter_covar_treat_reg if inlist(round,7) , outcomevar(Unfound_9YEL)  controls( $base_small ) fixedeffects(tp_strata_alt cg_strata)  filename(TableA4_attrition_x_base) label_first_row("Levels") add_ftest("yes")


*****************************************************************
////Apendix B: Additional results ************************************
****************************************************************
/// TABLE B1.-

*Main
multivar_reg_mat if inlist(round,7), outcomevar(cens_fam_asb_tyav) ///
                  controls(tpassonly cashassonly tpcashass $base) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(main)


*UnCensored ASB
multivar_reg_mat if inlist(round,7), outcomevar(uncens_fam_asb_tyav) ///
                  controls(tpassonly cashassonly tpcashass $base) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(main_uncens)


*Main Small
multivar_reg_mat if inlist(round,7), outcomevar(cens_fam_asb_tyav) ///
                  controls(tpassonly cashassonly tpcashass $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(main_small)



*FE_only
multivar_reg_mat if inlist(round,7), outcomevar(cens_fam_asb_tyav) ///
                  controls(tpassonly cashassonly tpcashass) fixedeffects(tp_strata_alt cg_strata)  ///
                  matname(main_FE_only)


*DML
multivar_reg_mat if inlist(round,7), outcomevar(cens_fam_asb_tyav) ///
                  controls(tpassonly cashassonly tpcashass $base_dml) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(main_dml)

*****Alternative index specs

multivar_reg_mat if inlist(round,7, 8), outcomevar(cens_fam_asb_e) ///
                  controls(tpassonly cashassonly tpcashass $base) fixedeffects(tp_strata_alt cg_strata) cluster(partid) ///
                  matname(pooled)


multivar_reg_mat if inlist(round,7), outcomevar(cens_fam_asb_cov_tyav) ///
                  controls(tpassonly cashassonly tpcashass $base) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(cov_weight)



mv_reg_table_nosum, resultsmat( ///
                         main                  ///
                         main_uncens           ///
                         main_small            ///
                         main_FE_only          ///
                         main_dml              ///
                         pooled                ///
                         cov_weight            ///
                         ) ///
                rowvars(tpassonly cashassonly tpcashass) ///
                stars novalues(yes) remove_sum(yes) ///
              specnames(`" "Main" "No top-coding \\ of ASB \\components" "Subset of \\ main \\ control vars." "Fixed-Effects \\ Only" "Double lasso \\ control vars." "2 obs. per \\ individual"  "Covariance \\ weighted \\ index" "') ///
              filename(TableB1_main_robustness)




///// TABLE B2.-  ***********************************

   foreach att in main 9010 8020 ///
   {

                         local appendix `att'
    if "`att'" == "main" local appendix

multivar_reg_mat if inlist(round,7), outcomevar(cens_fam_asb_tyav`appendix') ///
                  controls(tpassonly  cashassonly  tpcashass  $base) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(fam_asb_attb_`att')
}

*Evidently the trimming regression has not been done.
*Correcting it now:

*Restart the matrix with the format we want.
mat fam_asb_attb_t_lb = J(60,3,.)
mat rownames fam_asb_attb_t_lb = tpassonly cashassonly tpcashass $base


reg Unfound_9YEL tpassonly cashassonly tpcashass  if round == 7

*Define relevant delta (i.e. fraction that needs to be corrected in trimming exercise)
*For every treatment arm the comparison should be the control group.


*However because therapy has higher attrition, for that regression we need to trim the control group.
local delta_control        =  - (_b[tpassonly])
local delta_tpassonly      =  0
local delta_cashassonly    =  _b[cashassonly]
local delta_tpcashass      =  _b[tpcashass]


*Determine the N of obs that needs to be trimmed
foreach var_arm of varlist control tpassonly cashassonly tpcashass{

local this_delta =  `delta_`var_arm''
count if `var_arm' & round == 7
local N_trimmed_`var_arm' = -1*round(`this_delta' * r(N) , 1)

di "`N_trimmed_`var_arm''"

}


*Sort ASB by group
cap drop  inv_cens_fam_asb_tyav
gen inv_cens_fam_asb_tyav = -(cens_fam_asb_tyav)

foreach var_arm in tpassonly cashassonly tpcashass control {

cap drop  rank_`var_arm'_asb
cap drop  inv_rank_`var_arm'_asb
egen rank_`var_arm'_asb = rank(cens_fam_asb_tyav) if `var_arm' == 1 & round == 7
egen inv_rank_`var_arm'_asb = rank(inv_cens_fam_asb_tyav) if `var_arm' == 1 & round == 7

}

*Number 1 in rank_x_asb is LOWEST ASB.
*Number 1 in inv_rank_x_asb is HIGHEST ASB.


*Trim by creating a variable that takes 1 if the value is among the lowest N_g ASB values of each treatment arm.
*This takes away the "best" cases of a treatment arm and it makes treatment looks make less effective.
*Thus, it allows for a lower bound estimate.

cap drop  trim_arm_asb_tyav
gen trim_arm_asb_tyav = 0 if round == 7

foreach var_arm in tpassonly cashassonly tpcashass {

replace trim_arm_asb_tyav = 1  if rank_`var_arm'_asb <=  `N_trimmed_`var_arm''

}

*Trim by creating a variable that takes 1 if the value is among the highest N_g ASB values of the control group.
*This takes away the "worst" cases of the control arm and it makes treatment looks make less effective in relative terms.
*Thus, it allows for a lower bound estimate.

cap drop  trim_control_asb_tyav
gen trim_control_asb_tyav = 0 if round == 7
replace trim_control_asb_tyav = 1  if inv_rank_control_asb <=  `N_trimmed_control'


*Now we do the regressions. Note that we are always comparing one treatment arm with the control group.


local row_index = 1
foreach treat_arm in tpassonly cashassonly tpcashass {

if "`treat_arm'" == "tpassonly"        local trim_var trim_control_asb_tyav
if "`treat_arm'" != "tpassonly"        local trim_var trim_arm_asb_tyav

reghdfe cens_fam_asb_tyav  `treat_arm'   $base if round == 7   & `trim_var' == 0   &  (`treat_arm' == 1 | control == 1), vce(r)   a(tp_strata_alt cg_strata)

mat fam_asb_attb_t_lb[`row_index',1] = _b[`treat_arm']
mat fam_asb_attb_t_lb[`row_index',2] = _se[`treat_arm']
mat fam_asb_attb_t_lb[`row_index',3] = (2 * ttail(e(df_r), abs(_b[`treat_arm']/_se[`treat_arm'])))

local ++row_index

}

*Note: this matrix has results from 3 regressions on its first 3 rows and nothing on the rest of the rows.
*It is not comparable to the other regressions, but I am making this point evident by explicitly noting that
*nothing is being saved from the controls.

forval i = 4/60 {

mat fam_asb_attb_t_lb[`i',1] = .
mat fam_asb_attb_t_lb[`i',2] = .
mat fam_asb_attb_t_lb[`i',3] = .

}

    mv_reg_table_nosum, resultsmat(fam_asb_attb_main fam_asb_attb_9010 fam_asb_attb_8020 fam_asb_attb_t_lb ) ///
             stars  rowvars( tpassonly  cashassonly  tpcashass ) remove_sum("yes") ///
               novalues(no) specnames(`" "Main reg \\ (N=833)" "Impute .1 SD \\(N=999)" "Impute .2 SD \\(N=999)" "Trim \\(N = 393, 384, 383)" "') ///
              filename(TableB2_asb_attbounds)


//// TABLE B3.- ************************************

cap drop int_asb_c_tpassonly int_asb_c_cashassonly int_asb_c_tpcashass

gen int_asb_c_tpassonly=fam_asb_b*tpassonly
label variable int_asb_c_tpassonly "Therapy Only $\times$ Baseline ASB"

gen int_asb_c_cashassonly=fam_asb_b*cashassonly
label variable int_asb_c_cashassonly "Cash Only $\times$ Baseline ASB"

gen int_asb_c_tpcashass=fam_asb_b*tpcashass
label variable int_asb_c_tpcashass "Therapy + Cash $\times$ Baseline ASB"


multivar_reg_mat if inlist(round,7) & unfound_wave==0, outcomevar(cens_fam_asb_tyav) ///
                  controls(int_asb_c_tpassonly tpassonly int_asb_c_cashassonly cashassonly int_asb_c_tpcashass tpcashass fam_asb_b $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(fam_asb_het_vert)
multivar_reg_mat if inlist(round,7) & unfound_wave==0, outcomevar(fam_econ_tyav) ///
                  controls(int_asb_c_tpassonly tpassonly int_asb_c_cashassonly cashassonly int_asb_c_tpcashass tpcashass fam_asb_b $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(fam_econ_het_vert)
multivar_reg_mat if inlist(round,7) & unfound_wave==0, outcomevar(fam_identity_tyav) ///
                  controls(int_asb_c_tpassonly tpassonly int_asb_c_cashassonly cashassonly int_asb_c_tpcashass tpcashass fam_asb_b $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(fam_identity_het_vert)
multivar_reg_mat if inlist(round,7) & unfound_wave==0, outcomevar(timepref_tyav) ///
                  controls(int_asb_c_tpassonly tpassonly int_asb_c_cashassonly cashassonly int_asb_c_tpcashass tpcashass fam_asb_b $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(timepref_het_vert)
multivar_reg_mat if inlist(round,7) & unfound_wave==0, outcomevar(selfcontrolnolo_tyav) ///
                  controls(int_asb_c_tpassonly tpassonly int_asb_c_cashassonly cashassonly int_asb_c_tpcashass tpcashass fam_asb_b $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(selfcontrolnolo_het_vert)
multivar_reg_mat if inlist(round,7) & unfound_wave==0, outcomevar(fam_mental_tyav) ///
                  controls(int_asb_c_tpassonly tpassonly int_asb_c_cashassonly cashassonly int_asb_c_tpcashass tpcashass fam_asb_b $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(fam_mental_het_vert)
multivar_reg_mat if inlist(round,7) & unfound_wave==0, outcomevar(fam_network_tyav) ///
                  controls(int_asb_c_tpassonly tpassonly int_asb_c_cashassonly cashassonly int_asb_c_tpcashass tpcashass fam_asb_b $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(fam_network_het_vert)
multivar_reg_mat if inlist(round,7) & unfound_wave==0, outcomevar(subabuse_tyav) ///
                  controls(int_asb_c_tpassonly tpassonly int_asb_c_cashassonly cashassonly int_asb_c_tpcashass tpcashass fam_asb_b $base_small) fixedeffects(tp_strata_alt cg_strata) ///
                  matname(subabuse_het_vert)


label variable fam_asb_b "Baseline ASB"


mv_reg_table, resultsmat(fam_asb_het_vert fam_econ_het_vert fam_identity_het_vert timepref_het_vert selfcontrolnolo_het_vert fam_mental_het_vert fam_network_het_vert subabuse_het_vert) ///
              rowvars( tpassonly  cashassonly  tpcashass int_asb_c_tpassonly int_asb_c_cashassonly int_asb_c_tpcashass fam_asb_b ) ///
               stars novalues(no) ///
              specnames(`" "Antisocial \\ behavior" "Economic \\ variables" "Identity" "Time \\ prefences"  "Self-control \\Skills" "Mental \\ health" "Social \\ networks" "Substance \\ abuse"  "') ///
              filename(TableB3_het_c_appendix)


//// TABLE B4 & B5 .- ************************************


gen table_death_type = updated_death_type
replace table_death_type = "Health complications, drug aggravated" if updated_death_type == "Illness" & drug_indicator == 1
replace table_death_type = "Health complications, not drug aggravated" if updated_death_type == "Illness" & drug_indicator != 1
replace table_death_type = "Other" if updated_death_type == "Other"


cap gen other_death = .
cap gen illness_death_drug   = .
cap gen illness_death_nodrug = .
cap gen accidental_death = .
cap gen total_deaths = .
cap gen death_rate = .

label var accidental_death            "Accidental death"
label var illness_death_drug          "Health complications, drug aggravated"
label var illness_death_nodrug        "Health complications, not drug aggravated"
label var other_death                 "Other"
label var violent_death               "Violent death"
label var total_deaths                "Total \$ # \$ of deaths"
label var death_rate                  "Death proportion"



tab treatment_arm  if round == 7 , matcell(N_arm)
matrix N_arm_row = (N_arm' , 999)


gen rprt_table_death_type = table_death_type
replace rprt_table_death_type = "" if  rprt_table_death_type=="death to be classified"

gen attr_table_death_type = table_death_type
replace attr_table_death_type = "" if attrit_both_decease_9y != 1



foreach death_type in rprt attr {

qui tab `death_type'_table_death_type treatment_arm  if round == 7, matcell(deaths_table)

mata : st_matrix("B", rowsum(st_matrix("deaths_table")))
mat deaths_table = (deaths_table , B)

mata : st_matrix("C", colsum(st_matrix("deaths_table")))
mat deaths_table = (deaths_table \ C)

matrix prop_row = J(1, 5, .)
forvalues j = 1/5 {

matrix prop_row[1, `j'] = C[1, `j'] / N_arm_row[1, `j']

}


mat deaths_table = (deaths_table \ prop_row)

mat rownames deaths_table = accidental_death illness_death_drug illness_death_nodrug  violent_death other_death total_deaths death_rate

if "`death_type'" == "attr"  frmttable using outfiles/tables/TableB4_death_tab_`death_type' , statmat(deaths_table) varlabel tex fragment sdec(0\0\0\0\0\0\3) ctitle("Death type"  "Control"  "Therapy" "Cash"  "TP + Cash" "Total") replace nocenter
if "`death_type'" == "rprt"  frmttable using outfiles/tables/TableB5_death_tab_`death_type' , statmat(deaths_table) varlabel tex fragment sdec(0\0\0\0\0\0\3) ctitle("Death type"  "Control"  "Therapy" "Cash"  "TP + Cash" "Total") replace nocenter

}



//// TABLE B6 - B12.- ************************************

    gen patience_heading_tyav = .
    gen inconsistency_heading_tyav = .

    label variable patience_heading_tyav          "\quad{Variables obtained from patience games}"
    label variable inconsistency_heading_tyav     "\quad{Variables obtained from time inconsistency games}"



    label_itt_regression if inlist(round,7) &  unfound_wave==0, outcomevars(timepref        $timepref_main_ind)      controls($base) fixedeffects(tp_strata_alt cg_strata) terms(ny)  filename(TableB6_index_timep)
    label_itt_regression if inlist(round,7) &  unfound_wave==0, outcomevars(selfcontrolnolo $forwlook_main_ind)      controls($base) fixedeffects(tp_strata_alt cg_strata) terms(ny)  filename(TableB7_index_selfc)
    label_itt_regression if inlist(round,7) &  unfound_wave==0, outcomevars(fam_identity    $identity_main_ind)      controls($base) fixedeffects(tp_strata_alt cg_strata) terms(ny)  filename(TableB8_index_identi)
    label_itt_regression if inlist(round,7) &  unfound_wave==0, outcomevars(fam_mental      $mental_main_ind)        controls($base) fixedeffects(tp_strata_alt cg_strata) terms(ny)  filename(TableB9_index_mental)
    label_itt_regression if inlist(round,7) &  unfound_wave==0, outcomevars(subabuse        $subabuse_ind)           controls($base) fixedeffects(tp_strata_alt cg_strata) terms(ny)  filename(TableB10_index_abuse)
    label_itt_regression if inlist(round,7) &  unfound_wave==0, outcomevars(fam_network     $famnet_main_ind)        controls($base) fixedeffects(tp_strata_alt cg_strata) terms(ny)  filename(TableB11_index_famnet)
    label_itt_regression if inlist(round,7) &  unfound_wave==0, outcomevars(fam_econ        $econ_main_ind)          controls($base) fixedeffects(tp_strata_alt cg_strata) terms(ny)  filename(TableB12_index_econ)



//// TABLE B13.- ************************************

global tyav_secondary_outcomes ///
fam_econ_tyav                    ///
timepref_tyav                    ///
selfcontrolnolo_tyav             ///
fam_identity_tyav                ///
fam_mental_tyav                  ///
fam_subabuse_tyav                ///
fam_network_tyav


foreach outcome in $tyav_secondary_outcomes {


multivar_reg_mat if inlist(round,7), outcomevar(cens_fam_asb_tyav) ///
                  controls(`outcome') ///
                  matname(M_`outcome')

}

multivar_reg_mat if inlist(round,7), outcomevar(cens_fam_asb_tyav) ///
                  controls($tyav_secondary_outcomes) ///
                  matname(M_all_outcomes)


mv_reg_table, resultsmat( ///
                          M_fam_econ_tyav                    ///
                          M_timepref_tyav                    ///
                          M_selfcontrolnolo_tyav             ///
                          M_fam_identity_tyav                ///
                          M_fam_mental_tyav                  ///
                          M_fam_subabuse_tyav                ///
                          M_fam_network_tyav                 ///
                          M_all_outcomes  ///
                         ) ///
                rowvars($tyav_secondary_outcomes) ///
               stats(dvmean N) novalues(yes) ///
              stars specnames(`" " " " " " " " " " " " " " " "Multivariate \\ regression"   "') ///
              filename(TableB13_mediation_analysis)
